#! /usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import re
import HTMLParser
import urllib
import math

import sys


def loop_for_parse(t):
    type_dic = {'http': '1',
                'https': '2',
                'socks': '3'}
    result = []
    for i in range(50):
        url = 'http://tools.rosinstrument.com/raw_free_db.htm?'\
            + str(i) + '&t=' + type_dic[t]
        print "[*] Parsing %s now" % url
        try:
            proxy = parse_proxy_list(url)
        except:
            break
        result += proxy
    return result


def parse_proxy_list(url):
    r = requests.get(url)
    assert r.ok
    # ----------------------------------------
    # Simulate this js segment
    # Refered to: http://stackoverflow.com/questions/
    # 8269578/decode-javascript-sequence-into-python
    # function hideTxt(str){
    # var t=''; var s=unescape(str); var x=Math.round(Math.sqrt(36));
    # for(var i=0;i<s.length;i++)
    # t+=String.fromCharCode(s.charCodeAt(i)^(i%2?x:0));
    # document.write(t.replace(/\*/g,'<\/td><td>').replace(/\^/g,'" title="').
    # replace(/~/g,'<\/td><\/tr><tr class="dbeven"><td>')
    # .replace(/!/g,'<\/td><\/tr><tr class="dbodd"><td>')
    # .replace(/\|/g,'<\/td><td><a href="\/cgi-bin\/shdb\.pl\?key=')
    # );
    # }
    # hideTxt(
    # 'blablabla');
    # -->
    # </script>
    #----------------------------------------
    x = int(re.findall(u'sqrt\((\d+)\)', r.content)[0])
    x = int(math.sqrt(x))
    st = re.findall(u"hideTxt\(\n+'([^']+)'\);", r.content, re.S)[0]
    st = urllib.unquote(st)
    result = []
    for i, c in enumerate(st):
        result.append(chr(ord(c) ^ (x if i % 2 else 0)))
    t = "".join(result)
    # html实体解析
    h = HTMLParser.HTMLParser()
    t = h.unescape(t)
    # Strange '%2b' spot
    t = t.replace('%2b', '')
    # Strange `-` after `:`
    t = t.replace(':-', ':')
    proxy_list = re.findall('\|([^\^]+)\^', t)
    return proxy_list

if __name__ == '__main__':
    t = sys.argv[1]
    proxy_list = loop_for_parse(t)
    for i in range(len(proxy_list)):
        # print proxy_list[i]
        proxy_list[i] += '\n'
    with open('proxy_list/' + t + '_proxy_rosinstrument.txt', 'wb') as f:
        f.writelines(proxy_list)
